package com.action.attack
{
	import com.action.ActionAttack;
	import com.action.ActionStand;
	import com.logic.battle.BattleLogic;
	import com.logic.battle.BattleServer;
	import com.stage.item.Player;
	
	import flash.geom.Point;
	
	/**
	 * 普通攻击动作
	 * 有向前冲刺和向上冲刺
	 */
	public class ActionAttackNormal extends ActionAttack
	{
		protected var _upSpeed:Number; // 往上冲刺绝对距离
		protected var _speedDelay:int; // 位移延时帧数，大于0要延时
		protected var _distance:Number; // 水平冲刺绝对距离，也可能是后退
		protected var _currDis:Number;
		protected var _disPer:Number;
		protected var _isInit:Boolean = false;
		
		public function ActionAttackNormal(player:Player, data:Object)
		{
			super(player, data);
		}
		
		public override function start(dir:int=1, moveDir:int=0):void{
			_dir = dir;
			// TODO 根据技能做动作
			_player.act = _skillId;

			_speedDelay = 0;
			
			super.start(dir);
			
			if(_speedDelay <= 0){
				_upSpeed = 0;
				_speed = -5;
				_distance = 100;
				_currDis = 0;
				_disPer = Math.abs(_speed);
				_downSpeed = _upSpeed;
				_isInit = true;
			}
		}
		
		protected override function onLandGround():void{
			if(_actFinish && _currDis >= _distance){
				actionCallback();
			}
		}
		
		protected override function actionEnd(player:Player):void{
			_player.view.isStop = true;
			_actFinish = true;
		}
		
		protected override function enterFrame():void{
			// 如果是主机，添加各种检查
			if(BattleLogic.hostId == BattleLogic.aid){
				// 测试时，如果思attack
				if(!_actFinish){
					var sendBullet:Object = _player.view.getSendBullet();
					if(sendBullet && sendBullet.x1 < sendBullet.x2){
						var position:Point = _player.view.getPosition();
						// 有子弹的技能
						BattleServer.sendBullet(_player.data.aid, _skillId, _player.dir, _player.x + (sendBullet.x1 - position.x) * _player.dir, _player.y + (sendBullet.y1 - position.y));
					}
				}
				// 是skill时，没有子弹，直接身体接触的技能
				BattleServer.playerHitPlayer(this);
			}
			
			checkGround();
			
			_speedDelay--;
			if(_speedDelay <= 0 && _isInit == false){
				_upSpeed = -20;
				_speed = 20;
				_distance = 200;
				_currDis = 0;
				_disPer = Math.abs(_speed);
				_downSpeed = _upSpeed;
				_isInit = true;
			}else if(_speedDelay <= 0){
				if(_currDis + _disPer >= _distance){
					_player.x += dir * (_distance - _currDis);
					_currDis = _distance;
				}else{
					_player.x += dir * _speed;
					_currDis += _disPer;
				}
			}
		}
		
		public override function end():void{
			_player.view.isStop = false;
			super.end();
		}
	}
}